// pages/moreGroup/moreGroup.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group: [
      { id: 1, img: '../../img/banner.png', name: '张三', lack: '1', time: '02：30：50' },
      { id: 2, img: '../../img/banner.png', name: '李四', lack: '2', time: '06：20：40' },
      { id: 3, img: '../../img/banner.png', name: '老铁', lack: '3', time: '07：20：10' },
    ],
    cutdown: {
      'hh': 0,
      'dd': 0,
      'mm': 0,
      'ss': 0
    }, //倒计时
    other_cutdown: {
      'dd': 0,
      'mm': 0,
      'ss': 0
    }, //倒计时
    setintvalid: null,
    notcut: false, //是否结束
    groupInfo: [],
    goodsInfo: [],
    otherlist: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    group_id:'',
  },
  // 立即参团
  joinGroup(e) {
    let that = this;
    wx.navigateTo({
      url: '../joinGroup/joinGroup?sponsor_id=' + e.currentTarget.dataset.id + '&group_id=' + that.data.group_id + '&group_number=' + that.data.groupInfo.group_number,
    })
  },
  // 获取拼团详情
  getgroupDetail(group_id) {
    let that = this;
    wh.Post('/api/mall.group/detail', {
      group_id: group_id
    }, function (res) {
      console.log('砍价详情', res)
      clearInterval(that.setintvalid); //清除倒计时
      let thistime = new Date().getTime(); //判断时间
      let start_time = res.start_time + '000'; //精确到毫秒数 
      let end_time = res.group.end_time + '000'; //精确到毫秒数 
      if (thistime <= start_time) { //当前时间小于开始时间 活动未开始
        that.start_bargain(start_time, thistime, end_time);
      } else if (thistime < end_time) { //当前时间小于结束时间 活动进行中
        that.end_bargain(end_time, thistime);
      } else {
        that.setData({
          notcut: true
        }); //活动结束
      }
      that.setData({
        groupInfo: res.group,
        goodsInfo: res.goods
      })
    })
  },
  // 获取别人的拼团
  other(group_id) {
    let that = this,
      count_down = this.data.count_down,
     page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
    wh.Post('/api/mall.group/others?', {
      group_id: group_id,
      page: page,
      page_num: page_size
    }, function (res) {
      console.log('别人的拼团', res)
      for (var i = 0; i < res.length; i++) {
        res[i].count_down = that.timestampToTime(res[i].count_down)
      }
      let otherlist = that.data.otherlist.concat(res),
        count = res.length;
      if (page_size > count) {
        that.data.no_data = 1;
        if (otherlist.length > 3) {
          that.setData({
            onearth: true
          })
        }
      }
      that.setData({
        otherlist: otherlist
      })
    })
    }
  },
  //活动未开始
  start_bargain: function (start_time, thistime, end_time) {

    let that = this;
    let cha = start_time - thistime; //开始时间减去现在时间
    that.setData({
      cutdaowbefore: true
    });
    that.setintvalid = setInterval(function () {
      cha -= 1000;
      if (cha <= 0) {
        clearInterval(that.setintvalid);
        cha = 0;
        that.end_bargain(end_time, thistime);
      }
      that.time_meter(cha);
    }, 1000);
  },

  //活动已开始
  end_bargain: function (end_time, thistime) {
    let cha = end_time - thistime;
    let that = this;

    that.setData({
      cutdaowbefore: false
    })

    that.setintvalid = setInterval(function () {
      cha -= 1000;
      if (cha <= 0) {
        clearInterval(that.setintvalid);
        cha = 0;
        that.setData({
          notcut: true
        });
        return;
      }
      that.time_meter(cha)
    }, 1000);
  },
  //时间计算
  time_meter: function (timer) {
    let that = this;
    let dd = parseInt(timer / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    let hh = parseInt(timer / 1000 / 60 / 60 % 24, 10); //计算剩余的小时数
    let mm = parseInt(timer / 1000 / 60 % 60, 10); //计算剩余的分钟数
    let ss = parseInt(timer / 1000 % 60, 10); //计算剩余的秒数
    mm = that.checkTime(mm);
    ss = that.checkTime(ss);
    let cutdown = {
      'dd': dd,
      'hh': hh,
      'mm': mm,
      'ss': ss
    };
    that.setData({
      cutdown: cutdown
    })
  },
  checkTime: function (i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  },
  //时间戳
  timestampToTime: function (timestamp) {
    var date = new Date(timestamp * 1000),//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      Y = date.getFullYear() + '-',
      M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
      D = date.getDate() + ' ',
      h = date.getHours() + ' : ',
      m = date.getMinutes() + ' : ',
      s = date.getSeconds();
    return h + m + s;
  },
  // 发起拼团
  open() {
    let that = this,
      group_id = that.data.group_id;
    wh.Post('/api/mall.group/open', {
      group_id: group_id
    }, function (res) {
      console.log('开团', res)
      if (res.pay == 1) {
        wx.showToast({
          title: '您已经发起过该拼团了',
          icon: 'none',
          duration: 2000,
          complete: function () {
            setTimeout(function () {
              wx.navigateTo({
                url: '../joinGroup/joinGroup?sponsor_id=' + res.sponsor_id + '&group_id=' + group_id + '&group_number=' + that.data.groupInfo.group_number,
              })
            }, 2000);
          }
        })
      } else {
        let istype = 1;//用来判断是拼团进入支付页面的
        wx.navigateTo({
          url: '../submit/submit?istype=' + istype + '&user_gro_id=' + res.join_id,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.group_id)
    this.getgroupDetail(options.group_id)
    this.other(options.group_id)
    this.setData({
      group_id: options.group_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   let group_id = this.data.group_id;
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      otherlist: [],
    });
    this.other(group_id)
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size,
      group_id = this.data.group_id;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.other(group_id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

  },
})